package quick.post.viewer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Window.Type;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
import java.io.IOException;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import java.awt.Font;
import javax.swing.ListSelectionModel;

public class MyTwitterClient {

	private Twitter twitter;
	private final static String CONSUMER_KEY = "9Jln8MrrZKf1XyEAL3YiVg";
	private final static String CONSUMER_KEY_SECRET = "34ejeUKw42YeADuLg1eBLS09CDKYJNbrkcjiEcZgI";
	private JFrame frmMyTwitterClient;
	private JTable table;
	private JTextField textField;
	private JScrollPane timeLineScrollPane;

	private void initialize() {
		frmMyTwitterClient = new JFrame();
		frmMyTwitterClient.setTitle("My Twitter Client (Live)");
		frmMyTwitterClient.setType(Type.UTILITY);
		frmMyTwitterClient.setResizable(false);
		frmMyTwitterClient.setBounds(100, 100, 762, 300);
		frmMyTwitterClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmMyTwitterClient.getContentPane().setLayout(null);

		timeLineScrollPane = new JScrollPane();
		timeLineScrollPane.setEnabled(false);
		timeLineScrollPane.setBounds(10, 79, 736, 152);
		frmMyTwitterClient.getContentPane().add(timeLineScrollPane);

		table = new JTable();
		table.setSurrendersFocusOnKeystroke(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setEnabled(false);
		table.setShowHorizontalLines(false);
		table.setShowGrid(false);
		table.setFont(new Font("Tahoma", Font.PLAIN, 10));
		table.setFillsViewportHeight(true);
		table.setModel(new DefaultTableModel(new Object[][] { { null } },
				new String[] { "Timeline update" }));
		timeLineScrollPane.setViewportView(table);

		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					getTimeline(twitter);
				} catch (TwitterException e) {
					e.printStackTrace();
					e.getErrorMessage().toString();
				}
			}
		});
		
		btnRefresh.setBounds(657, 242, 89, 23);
		frmMyTwitterClient.getContentPane().add(btnRefresh);

		textField = new JTextField();
		textField.setBounds(10, 30, 578, 20);
		frmMyTwitterClient.getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblRecentUpdatedTimeline = new JLabel("Recent Updated Timeline");
		lblRecentUpdatedTimeline.setBounds(10, 61, 151, 14);
		frmMyTwitterClient.getContentPane().add(lblRecentUpdatedTimeline);

		JButton btnUpdateStatus = new JButton("Update Status");
		btnUpdateStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				commitUpdateStatus();

			}

		});

		btnUpdateStatus.setBounds(615, 29, 131, 23);
		frmMyTwitterClient.getContentPane().add(btnUpdateStatus);

		JLabel lblWhatYouDoing = new JLabel("What you doing now ?");
		lblWhatYouDoing.setBounds(10, 11, 138, 14);
		frmMyTwitterClient.getContentPane().add(lblWhatYouDoing);

		try {
			start();
		} catch (TwitterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void commitUpdateStatus() {
		try {
			updateStatus(twitter, textField.getText());
			JOptionPane.showMessageDialog(null, "Status updated sucessfuly..");
			textField.setText("");
			getTimeline(twitter);
		} catch (TwitterException e) {

			e.printStackTrace();
		}
	}

	public void start() throws TwitterException, IOException {

		twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

		String accessToken = getSavedAccessToken();
		String accessTokenSecret = getSavedAccessTokenSecret();
		AccessToken oathAccessToken = new AccessToken(accessToken,
				accessTokenSecret);

		twitter.setOAuthAccessToken(oathAccessToken);

	}

	private void updateStatus(Twitter twitter, String txt)
			throws TwitterException {
		twitter.updateStatus(txt);
	}

	private void getTimeline(Twitter twitter) throws TwitterException {

		DefaultTableModel tabModel = new DefaultTableModel();
		tabModel.addColumn("Timeline ");

		table = new JTable(tabModel);
		ResponseList<Status> list = twitter.getHomeTimeline();
		for (Status each : list) {

			Object[] rowData = { "@" + each.getUser().getName() + ": -  "
					+ each.getText() };
			tabModel.addRow(rowData);

			timeLineScrollPane.setViewportView(table);
		}

	}

	private String getSavedAccessTokenSecret() {
		return "QZhdCKqfsKyYUuDXxT5H0K672or5zsjLiRJRhf7FJI";
	}

	private String getSavedAccessToken() {
		return "469823542-HxX3DWtLa2Khd97I0qP6F4jer3oo27YFycxSMKcT";
	}

	public MyTwitterClient() {
		initialize();
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyTwitterClient window = new MyTwitterClient();
					window.frmMyTwitterClient.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
